package com.star.links.board;

import com.star.links.Utils.ImageUtils;
import com.star.links.configure.PlayConfigure;
import com.star.links.views.Block;
import com.star.links.views.BlockImage;

import java.util.List;

public abstract class AbstractBoard {

        // 定义一个抽象方法, 让子类去实现
        protected abstract List<Block> createPieces(PlayConfigure config, Block[][] blocks);

    /**
     * 初始化二维数组
     * @param config
     * @return
     */
    public Block[][] create(PlayConfigure config)
        {
            // 创建Piece[][]数组,x是列坐标，y是行坐标
            Block[][] blocks = new Block[config.getColumnNum()][config.getRowNum()];
            // 返回非空的Piece集合, 该集合由子类去创建
            List<Block> notNullPieces = createPieces(config, blocks);
            // 根据非空Piece对象的集合的大小来取图片
            int speciesNum;
            switch (config.getDifficulty()){
                case 0:
                    speciesNum=10;
                    break;
                case 1:
                    speciesNum=15;
                    break;
                case 2:
                    speciesNum=25;
                    break;
                default:
                    speciesNum=10;
                    break;
            }
            List<BlockImage> playImages = ImageUtils.getPlayImages(config.getContext(),speciesNum, notNullPieces.size());
            // 所有图片的宽、高都是相同的
            int imageWidth = playImages.get(0).getImage().getWidth();
            int imageHeight = playImages.get(0).getImage().getHeight();
            // 遍历非空的Piece集合
            for (int i = 0; i < notNullPieces.size(); i++)
            {
                // 依次获取每个Piece对象,并将图片给放入
                Block block = notNullPieces.get(i);
                block.setImage(playImages.get(i));
                // 计算每个方块左上角的X、Y座标
                block.setLocationX(block.getIndexX() * imageWidth + config.getFirstImageX());
                block.setLocationY(block.getIndexY() * imageHeight + config.getFirstImageY());
                // 将该方块对象放入方块数组的相应位置处
                blocks[block.getIndexX()][block.getIndexY()] = block;
            }
            return blocks;
        }

}
